package me.numark.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import butterknife.Bind;
import butterknife.ButterKnife;
import me.numark.R;
import me.numark.activities.SearchActivity;
import me.numark.utils.Constants;
import me.numark.utils.Global;
import me.numark.view.PagerSlidingTabStrip;

public class DiscoverFragment extends Fragment implements
        //RadioGroup.OnCheckedChangeListener,
        ViewPager.OnPageChangeListener {

    @Bind(R.id.toolbar_title)
    TextView textTitle;

    @Bind(R.id.pager)
    ViewPager pager;

    @Bind(R.id.radio_left)
    RadioButton leftRadio;
    @Bind(R.id.show_tabs)
    PagerSlidingTabStrip tabs;
    @Bind(R.id.radio_right)
    RadioButton rightRadio;

    @Bind(R.id.radio_group)
    RadioGroup radioGroup;

    @Bind(R.id.right_image)
    ImageView rightImage;

    DiscoverFragmentPageAdapter mAdapter;

    BaseListFragment[] fragments = new BaseListFragment[5];

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_tabs, container, false);
        ButterKnife.bind(this, v);
        textTitle.setVisibility(View.VISIBLE);
        textTitle.setText(getString(R.string.discovery));
        mAdapter = new DiscoverFragmentPageAdapter();
        pager.setAdapter(mAdapter);
        pager.addOnPageChangeListener(this);
        radioGroup.setVisibility(View.GONE);
        /*leftRadio.setText(mAdapter.getPageTitle(0));
        rightRadio.setText(mAdapter.getPageTitle(1));
        radioGroup.setOnCheckedChangeListener(this);*/
        tabs.setVisibility(View.VISIBLE);
        tabs.setShouldExpand(true);
        tabs.setIndicatorColorResource(R.color.btn_green);
        tabs.setUnderlineColorResource(R.color.news_card_text1);
        tabs.setCheckedTextColorResource(R.color.btn_green);
        tabs.setTextColor(R.color.gray2);
        tabs.setViewPager(pager);
        pager.setOffscreenPageLimit(3);
        rightImage.setVisibility(View.VISIBLE);
        rightImage.setImageResource(R.drawable.action_bar_search);
        rightImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), SearchActivity.class));
            }
        });
        return v;
    }

    /*@Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (checkedId == R.id.radio_right) {
            pager.setCurrentItem(1);
        } else {
            pager.setCurrentItem(0);
        }
    }*/

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        tabs.onPageScrolled(position, positionOffset, positionOffsetPixels);
    }

    @Override
    public void onPageSelected(int position) {
        /*if (i == 1) {
            radioGroup.check(R.id.radio_right);
        } else {
            radioGroup.check(R.id.radio_left);
        }*/
        tabs.onPageSelected(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {
        tabs.onPageScrollStateChanged(state);
    }

    private class DiscoverFragmentPageAdapter extends FragmentPagerAdapter {

        public DiscoverFragmentPageAdapter() {
            super(getChildFragmentManager());
        }
        public String getPageTitle(int i){
            String title = null;
            switch (i){
                case 0:
                    title = "热文";
                    break;
                case 1:
                    title = "商业";
                    break;
                case 2:
                    title = "科技";
                    break;
                case 3:
                    title = "人文";
                    break;
                case 4:
                    title = "娱乐";
                    break;
            }
            return title;
        }
        @Override
        public Fragment getItem(int i) {
            if (fragments[i] == null) {
                switch (i) {
                    case 0:
                        fragments[i] = FeedFragment.newInstance(Constants.FEEDTYPE_TAG_HOT,"0");

                        break;
                    case 1:
                        fragments[i] = FeedFragment.newInstance(Constants.FEEDTYPE_TAG_HOT,"1");

                        break;
                    case 2:
                        fragments[i] = FeedFragment.newInstance(Constants.FEEDTYPE_TAG_HOT,"2");
                        break;
                    case 3:
                        fragments[i] = FeedFragment.newInstance(Constants.FEEDTYPE_TAG_HOT,"3");

                        break;
                    case 4:
                        fragments[i] = FeedFragment.newInstance(Constants.FEEDTYPE_TAG_HOT,"4");
                        break;
                    default:
                        break;
                }
            }
            return fragments[i];
        }

        @Override
        public int getCount() {
            return 5;
        }

        /*@Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return getString(R.string.new_timeline);
                case 1:
                    return getString(R.string.hot_timeline);
                default:
                    return "";
            }
        }*/
    }

}
